<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    label-width="120px"
    v-loading="formLoading"
  >
    
   <el-divider content-position="left">人脸认证服务协议</el-divider>
     <el-col :span="24" class="mr-12">
   	<el-form-item>
   	  <Editor v-model="formData.facialAgreement" height="500px" />
   	</el-form-item>
     </el-col>
  </el-form>
  <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
</template>

<script setup lang="ts">
import * as BasicSettingApi from '@/api/blind/basicSetting'

defineOptions({ name: 'FacialAgreementtSetting' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const formRef = ref() // 表单 Ref
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('create') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  commissionRatio: 0,
  effectiveTime: 0,
  status: undefined
})

const formRules = reactive({
  commissionRatio: [{ required: true, message: '分销佣金比例不能为空', trigger: 'blur' }],
  effectiveTime: [{ required: true, message: '分销有效时间不能为空', trigger: 'blur' }]
})

/** 查询列表 */
const getData = async () => {
  const data = await BasicSettingApi.getBasicSetting(1)
  formData.value = data
  formType.value = data ? 'update' : 'create'
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true

  try {
    const data = formData.value as unknown as BasicSettingApi.BasicSettingVO

    if (formType.value === 'create') {
      await BasicSettingApi.createBasicSetting(data)
      message.success(t('common.createSuccess'))
    } else {
      await BasicSettingApi.updateBasicSetting(data)
      message.success(t('common.updateSuccess'))
    }
    // 发送操作成功的事件
    emit('success')
    getData()
  } finally {
    formLoading.value = false
  }
}
watch(formData, (newVal) => {
  formData.value.commissionRatio = newVal.commissionRatio
  formData.value.effectiveTime = newVal.effectiveTime
})

/** 初始化 **/
onMounted(() => {
  getData()
})
</script>
